/*
build NIBRS dataset .do file 
files used: NIBRS-`y'-Data.dta where y{1991:2016}, ORI_FIPS_CROSSWALK_clean.dta
files created: NIBRS_Data_1991_2016_ori.dta
programs used: build_NIBRS_ORI

Data comes from the Extract Files, available for download here: 
	https://www.icpsr.umich.edu/web/NACJD/series/00128/studies

What code does:
	creates basic daily homicide and assault counts for each NIBRS year
	collapses data to ORI day level
	merges data with ORI FIPS crosswalk
	saves collapsed data as NIBRS_`y'_condensed.dta" where y{1991:2016}
	appends all collapsed years
	saves appended data as NIBRS_Data_1991-2016.dta
*/
*******************************************************************************

forvalues y = 1991(1)2016 {
	use "Raw Data/NIBRS/Crime_data/NIBRS-`y'-Data.dta", clear

	*drop missing values
	drop if V20061 ==.
	
	*create homicide variable
	gen homicide = 0
	replace homicide = 1 if inlist(V20061, 91, 93)
	label var homicide "homicide offenses"
	
	*create negligent manslaughter variable
	gen n_manslaughter = 0
	replace n_manslaughter = 1 if V20061 == 92
	
	*create assault variable
	gen assault = 0
	replace assault = 1 if inlist(V20061, 131, 132, 133)
	label var assault "assault offenses"
	
	gen agg_assault = 0
	replace agg_assault = 1 if V20061 == 131
	label var agg_assault "aggravated assault"
	
	gen simple_assault = 0
	replace simple_assault = 1 if V20061 == 132
	label var simple_assault "simple assault"
	
	gen intimidation = 0
	replace intimidation = 1 if V20061 == 133
	label var intimidation "intimidation assault"
	
	*indicate if gun was involved
	gen crime_gun = 0
	replace crime_gun = 1 if inlist(V20171, 110, 111, 120, 121, 130, 131, 140, 141, 150, 151)
	
	*creat gun-crime interactions
	foreach var of varlist homicide assault agg_assault simple_assault intimidation {
		gen `var'_gun = 1 if `var' == 1 & crime_gun == 1
		replace `var'_gun = 0 if `var'_gun == .
		
		gen `var'_ngun = 1 if `var' == 1 & crime_gun == 0
		replace `var'_ngun = 0 if `var'_ngun == .
	}
	
	*rename year variable for 2015
	if `y' == 2015{
		rename BH041 B3011
	}
	
	cap rename B2005 population1
	cap rename BH019 population1
	
	cap rename B2009 population2
	cap rename BH023 population2

	cap rename B2013 population3
	cap rename BH027 population3
	
	egen population = rsum(population1 population2 population3)
	
	
	** TIME OF DAY **
	
	gen morning = 0
	replace morning = . if V1007 == . 
	replace morning = 1 if V1007 >= 6 & V1007 < 12
	gen afternoon = 0 
	replace afternoon = . if V1007 == .
	replace afternoon = 1 if V1007 >= 12 & V1007 < 18
	gen night = 0
	replace night = . if V1007 == .
	replace night = 1 if V1007 >= 18 & V1007 < 24
	replace night = 1 if V1007 >= 0 & V1007 < 6
	
	gen homicide_morning = 0
	replace homicide_morning = 1 if homicide == 1 & morning == 1
	replace homicide_morning = . if homicide == 1 & morning == .

	gen homicide_afternoon = 0
	replace homicide_afternoon = 1 if homicide == 1 & afternoon == 1
	replace homicide_afternoon = . if homicide == 1 & afternoon == .

	gen homicide_night = 0
	replace homicide_night = 1 if homicide == 1 & night == 1
	replace homicide_night = . if homicide == 1 & night == .

	** LOCATIONS **
	
	gen at_home = 0
	replace at_home = 1 if V20111 == 20
	replace at_home = . if V20111 == . | V20111 < 0
	
	gen outside = 0
	replace outside = 1 if V20111 == 13 |  V20111 == 18 |  V20111 == 10 |  V20111 == 16 
	replace outside = . if V20111 == .  | V20111 < 0
	
	gen other = 0 
	replace other = 1 if at_home == 0 & outside == 0 
	replace other = . if V20111 == . | V20111 < 0
	
	gen homicide_at_home = 0
	replace homicide_at_home = 1 if homicide == 1 & at_home == 1
	replace homicide_at_home = . if homicide == 1 & at_home == .

	gen homicide_outside = 0
	replace homicide_outside = 1 if homicide == 1 & outside == 1
	replace homicide_outside = . if homicide == 1 & outside == .

	gen homicide_other = 0
	replace homicide_other = 1 if homicide == 1 & other == 1
	replace homicide_other = . if homicide == 1 & other == .

	#delimit ;
	local collapse_list
	homicide
	assault
	agg_assault
	simple_assault
	intimidation
	n_manslaughter
	homicide_gun
	assault_gun
	agg_assault_gun
	homicide_ngun
	assault_ngun 
	agg_assault_ngun 
	homicide_morning 
	homicide_afternoon
	homicide_night
	homicide_at_home 
	homicide_outside 
	homicide_other
	;
	#delimit cr
	
	collapse (mean) population (sum) `collapse_list', by(ORI INCDATE STATE B3011)
	
	tempfile NIBRS`y'
	save `NIBRS`y''

}

** Append allyears

use `NIBRS1991', clear
forvalues y = 1992(1)2016 {
	append using `NIBRS`y''
}

** Merge with ORI FIPS crosswalk
rename ORI ori9
merge m:1 ori9 using "Raw Data/NIBRS/Crosswalk_Data/ORI_FIPS_CROSSWALK_clean_NIBRS.dta"

keep if _merge == 3
drop _merge

** Create date variables
tostring INCDATE, replace
gen date = date(INCDATE,"YMD")
format date %dM_d,_CY
label var date "incident date"

gen year = year(date)
gen month = month(date)
gen day = day(date)

** Drop observations with missing dates
drop if date == .

** Rearrange
order STATE ori9 date

merge m:1 fips using "Raw Data/County Lat Lon/us_county_latlng.dta"
keep if _m != 2
drop _m

** Replace two counties whose fips codes changed
replace fips = 51059 if fips == 51610
replace fips = 51163 if fips == 51678

save "Processed Data/NIBRS_1991_2016.dta", replace	
